/*
 * Copyright 2010 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
package org.nicocube.wol.game.client.sub;

import java.io.Serializable;
import java.util.Date;

import org.nicocube.gwt.util.client.form.CheckBoxField;
import org.nicocube.gwt.util.client.form.DateField;
import org.nicocube.gwt.util.client.form.Form;
import org.nicocube.gwt.util.client.form.FormVisitor;
import org.nicocube.gwt.util.client.form.GenderField;
import org.nicocube.gwt.util.client.form.PasswordBoxField;
import org.nicocube.gwt.util.client.form.TextBoxField;
import org.nicocube.gwt.util.client.gender.Gender;
import org.nicocube.gwt.util.client.pubsub.AutoPublisher;
import org.nicocube.gwt.util.client.pubsub.AutoPublisherImpl;
import org.nicocube.gwt.util.client.pubsub.Subscriber;

import com.google.gwt.i18n.client.Constants;

public class PlayerForm implements Form,Serializable {
	private static final long serialVersionUID = 1L;

	private String login;
	private String password;
	private String retypePw;
	private String email;
	private Date dateOfBirth;
	private Gender gender;
	private String country;
	private String language;
	private boolean acceptNewsLetter;
	private boolean acceptPromotion;
	private boolean acceptPartner;

	private transient AutoPublisher<Form> publisherImpl;
	
	public PlayerForm() {
		publisherImpl = new AutoPublisherImpl<Form>(this);
	}
	
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
		publish();
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
		publish();
	}
	public String getRetypePw() {
		return retypePw;
	}
	public void setRetypePw(String retypePw) {
		this.retypePw = retypePw;
		publish();
	}
	public boolean checkPassword() {
		return password == retypePw;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
		publish();
	}
	public Date getDateOfBirth() {
		return dateOfBirth;
	}
	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
		publish();
	}
	public Gender getGender() {
		return gender;
	}
	public void setGender(Gender gender) {
		this.gender = gender;
		publish();
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
		publish();
	}
	public String getLanguage() {
		return language;
	}
	public void setLanguage(String language) {
		this.language = language;
		publish();
	}
	public boolean isAcceptNewsLetter() {
		return acceptNewsLetter;
	}
	public void setAcceptNewsLetter(boolean acceptNewsLetter) {
		this.acceptNewsLetter = acceptNewsLetter;
		publish();
	}
	public boolean isAcceptPromotion() {
		return acceptPromotion;
	}
	public void setAcceptPromotion(boolean acceptPromotion) {
		this.acceptPromotion = acceptPromotion;
		publish();
	}
	public boolean isAcceptPartner() {
		return acceptPartner;
	}
	public void setAcceptPartner(boolean acceptPartner) {
		this.acceptPartner = acceptPartner;
		publish();
	}

	@Override
	public void visitFormFields(Constants constants, FormVisitor formVisitor) {
		formVisitor.visit(new TextBoxField("login",this,constants) {
			@Override
			protected void setInForm(String value) {
				setLogin(value);
			}
			@Override
			protected String getInForm() {
				return getLogin();
			}
		});
		formVisitor.visit(new PasswordBoxField("password",this,constants) {
			@Override
			protected void setInForm(String value) {
				setPassword(value);
			}
			@Override
			protected String getInForm() {
				return getPassword();
			}
		});
		formVisitor.visit(new PasswordBoxField("retypePw",this,constants) {
			@Override
			protected void setInForm(String value) {
				setRetypePw(value);
			}
			@Override
			protected String getInForm() {
				return getRetypePw();
			}
		});
		formVisitor.visit(new TextBoxField("email",this,constants) {
			@Override
			protected String getInForm() {
				return getEmail();
			}
			@Override
			protected void setInForm(String value) {
				setEmail(value);
			}
		});
		formVisitor.visit(new DateField("dateOfBirth",this,constants) {
			@Override
			protected Date getInForm() {
				return getDateOfBirth();
			}
			@Override
			protected void setInForm(Date value) {
				setDateOfBirth(value);
			}
		});
		formVisitor.visit(new GenderField("gender",this,constants) {
			@Override
			protected Gender getInForm() {
				return getGender();
			}
			@Override
			protected void setInForm(Gender value) {
				setGender(value);
			}
		});		
		formVisitor.visit(new TextBoxField("country",this,constants) {
			@Override
			protected String getInForm() {
				return getCountry();
			}
			@Override
			protected void setInForm(String value) {
				setCountry(value);
			}
		});
		formVisitor.visit(new TextBoxField("language",this,constants) {
			@Override
			protected void setInForm(String value) {
				setLanguage(value);
			}
			@Override
			protected String getInForm() {
				return getLanguage();
			}
		});
		formVisitor.visit(new CheckBoxField("acceptNewsLetter",this,constants) {
			@Override
			protected Boolean getInForm() {
				return isAcceptNewsLetter();
			}
			@Override
			protected void setInForm(Boolean value) {
				setAcceptNewsLetter(value);
			}
			
		});
		formVisitor.visit(new CheckBoxField("acceptPromotion",this,constants) {
			@Override
			protected Boolean getInForm() {
				return isAcceptPromotion();
			}
			@Override
			protected void setInForm(Boolean value) {
				setAcceptPromotion(value);
			}
			
		});
		formVisitor.visit(new CheckBoxField("acceptPartner",this,constants) {
			@Override
			protected Boolean getInForm() {
				return isAcceptPartner();
			}
			@Override
			protected void setInForm(Boolean value) {
				setAcceptPartner(value);
			}
		});
	}
	
	@Override
	public void publish() {
		publisherImpl.publish();
	}

	@Override
	public String toString() {
		return 
		"login="+login+
		",password="+password+
		",retypePw="+retypePw+
		",email="+email+
		",dateOfBirth="+dateOfBirth+
		",gender="+gender+
		",country="+country+
		",language="+language+
		",acceptNewsLetter="+acceptNewsLetter+
		",acceptPromotion="+acceptPromotion+
		",acceptPartner="+acceptPartner;

	}

	@Override
	public void subscribe(Subscriber<Form> subscriber) {
		publisherImpl.subscribe(subscriber);
	}

	@Override
	public void unsubscribe(Subscriber<Form> subscriber) {
		publisherImpl.unsubscribe(subscriber);
	}

	@Override
	public void unsubscribeAll() {
		publisherImpl.unsubscribeAll();
	}

}
